using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Utility;
using Entities;
using BUS;
public partial class Admin_SanPham_DanhSachSanPham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
            LoadData();
        }
    }

    #region"LoadData()
    public void LoadData()
    {

        pager.PageSize = 5;
        Criteria c = new Criteria();
        if (txt_TenSP.Text != "")
        {
            c.add(Condition.AND, tbSanPhamColumns.TenSP, Condition.LIKE, txt_TenSP.Text);
        }
        if (ddl_DoanhMuc.SelectedValue != "0")
        {
            c.add(Condition.AND, tbSanPhamColumns.DanhMuc, Condition.EQUAL, int.Parse(ddl_DoanhMuc.SelectedValue));
        }

        if (ddl_DoanhNghiep.SelectedValue != "0")
        {
            c.add(Condition.AND, tbSanPhamColumns.DoanhNghiep, Condition.EQUAL, int.Parse(ddl_DoanhNghiep.SelectedValue));
        }

        if (ddl_TinhTrang.SelectedValue != "0")
        {
            c.add(Condition.AND, tbSanPhamColumns.TinhTrangSP, Condition.EQUAL, int.Parse(ddl_TinhTrang.SelectedValue));
        }
        int total = 0;
        tbSanPhamCollection sanPhamCollection =
            tbSanPhamBUS.Get(c.Criter, pager.CurrentIndex, pager.PageSize,
            tbSanPhamColumns.MaSP.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = sanPhamCollection;
        Repeater1.DataBind();
        pager.ItemCount = total;

    }
    #endregion

    #region"LoadDropDownList"
    public void LoadDropDownList()
    {

        tbDanhMucCollection danhMucCollection = tbDanhMucBUS.GetALL();
        ddl_DoanhMuc.DataTextField = tbDanhMucColumns.TenDanhMuc.ToString();
        ddl_DoanhMuc.DataValueField = tbDanhMucColumns.MaDM.ToString();
        ddl_DoanhMuc.DataSource = danhMucCollection;
        ddl_DoanhMuc.DataBind();
        ddl_DoanhMuc.Items.Insert(0, new ListItem("All", "0"));

        tbDoanhNghiepCollection danhNghiepCollection = tbDoanhNghiepBUS.GetALL();
        ddl_DoanhNghiep.DataTextField = tbDoanhNghiepColumns.TenDN.ToString();
        ddl_DoanhNghiep.DataValueField = tbDoanhNghiepColumns.MaDN.ToString();
        ddl_DoanhNghiep.DataSource = danhNghiepCollection;
        ddl_DoanhNghiep.DataBind();
        ddl_DoanhNghiep.Items.Insert(0, new ListItem("All", "0"));

        tbTinhTrangSPCollection tinhTrangSPCollection = tbTinhTrangSPBUS.GetAll();
        ddl_TinhTrang.DataTextField = tbTinhTrangSPColumns.TenTT.ToString();
        ddl_TinhTrang.DataValueField = tbTinhTrangSPColumns.MaTT.ToString();
        ddl_TinhTrang.DataSource = tinhTrangSPCollection;
        ddl_TinhTrang.DataBind();
        ddl_TinhTrang.Items.Insert(0, new ListItem("All","0"));
    }
    #endregion

    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
     
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
        e.Item.ItemType == ListItemType.Item)
        {
            tbSanPham sanPham = (tbSanPham)e.Item.DataItem;
            HyperLink hyp_TenSP = (HyperLink)e.Item.FindControl("hyp_TenSP");
            Literal lit_DanhMuc = (Literal)e.Item.FindControl("lit_DanhMuc");
            Image img_HinhAnh = (Image)e.Item.FindControl("img_HinhAnh");
            Literal lit_DoanhNghiep = (Literal)e.Item.FindControl("lit_DoanhNghiep");
            Literal lit_TinhTrang = (Literal)e.Item.FindControl("lit_TinhTrang");
            Literal lit_NhanVien = (Literal)e.Item.FindControl("lit_NhanVien");

              hyp_TenSP.Text = sanPham.TenSP;
            hyp_TenSP.NavigateUrl = "~/Admin/SanPham/ChiTietSanPham.aspx?id=" + sanPham.MaSP.ToString();

            tbDanhMuc danhMuc = tbDanhMucBUS.GetByID(sanPham.DanhMuc);
            lit_DanhMuc.Text = danhMuc.TenDanhMuc;

            tbHinhAnh cthinhanh = tbHinhAnhBUS.GetByMaSP(sanPham.MaSP);
            if (cthinhanh == null)
            {
                img_HinhAnh.ImageUrl = "~/Photos/Thumb/" + "no_images.jpg";
            }
            else
            {
                tbHinhAnh hinhAnh = tbHinhAnhBUS.GetByID(cthinhanh.MaHA);
                img_HinhAnh.ImageUrl = "~/Photos/Thumb/" + hinhAnh.Link;
 
            }
      

        

            tbDoanhNghiep doanhNghiep = tbDoanhNghiepBUS.GetByID(sanPham.DoanhNghiep);
            lit_DoanhNghiep.Text = doanhNghiep.TenDN;


            tbTinhTrangSP tinhTrang = tbTinhTrangSPBUS.GetByID(sanPham.TinhTrangSP);
            lit_TinhTrang.Text = tinhTrang.TenTT;

            tbNhanVien nhanVien = tbNhanVienBUS.GetByID(sanPham.NhanVien);
            lit_NhanVien.Text = nhanVien.TenNV;

            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "~/Admin/SanPham/CapNhatSanPham.aspx?id=" + sanPham.MaSP.ToString();
        }
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                tbHinhAnhCollection cthinhAnhCollection = tbHinhAnhBUS.GetByALLMaSP(id);
                if (cthinhAnhCollection!=null)
                {
                    for (int j = 0; j < cthinhAnhCollection.Count; j++)
                    {
       

                        tbHinhAnh hinhAnh = tbHinhAnhBUS.GetByID(cthinhAnhCollection[j].MaHA);

                        System.IO.File.Delete(Server.MapPath("~/Photos/" + hinhAnh.Link));
                        System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + hinhAnh.Link));
                        tbHinhAnhBUS.Delete(cthinhAnhCollection[j].MaHA);

                    }
                }

             
            tbSanPhamBUS.Delete(id);
            }
        }
             Response.Redirect(Request.RawUrl);
    }
}
